package com.financial.sys.service.user.Impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.CannedAccessControlList;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.financial.model.pojo.TUserAccount;
import com.financial.model.pojo.TUserInfo;
import com.financial.model.pojo.TUserWallet;
import com.financial.sys.mapper.user.TUserAccountMapper;
import com.financial.sys.mapper.user.TUserInfosMapper;
import com.financial.sys.mapper.user.TUserWalletsMapper;
import com.financial.sys.service.user.TUserInfosService;
import com.financial.sys.util.ConstantPropertiesUtil;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@Service
public class TUserInfosServiceImpl extends ServiceImpl<TUserInfosMapper, TUserInfo> implements TUserInfosService {

    @Autowired
    private TUserWalletsMapper tUserWalletMapper;
	@Autowired
	private TUserAccountMapper tUserAccountMapper;

    @Override
    public TUserInfo selectUserInfo(String accountId) {
        QueryWrapper<TUserInfo> tUserInfoQueryWrapper=new QueryWrapper<>();
        tUserInfoQueryWrapper.eq("account_id",accountId);
        TUserInfo tUserInfo=baseMapper.selectOne(tUserInfoQueryWrapper);
        return tUserInfo;
    }

    @Override
    public int UserInfoUpdate(TUserInfo tUserInfo) {
        int i=baseMapper.updateById(tUserInfo);
	    String id = tUserInfo.getAccountId();
	    TUserAccount tUserAccount = tUserAccountMapper.selectById(id);
	    tUserAccount.setFillUserinfo(1);
	    tUserAccountMapper.updateById(tUserAccount);
	    String accountId = tUserInfo.getAccountId();
        QueryWrapper<TUserWallet>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account_id",accountId);
        TUserWallet tUserWallet=tUserWalletMapper.selectOne(queryWrapper);
        if(tUserWallet!=null){
            //获得用户的信用得分
            int creditScore=tUserWallet.getCreditScore();
            //为0表示第一次完善个人信息
            if(creditScore==0){
                //为用户设置100分的信用得分，
                tUserWallet.setCreditScore(100);
                tUserWalletMapper.updateById(tUserWallet);
            }
        }
        return i;
    }

	@Override
	public String upload(MultipartFile file) {
		//获取阿里云存储相关常量
		String endPoint = ConstantPropertiesUtil.END_POINT;
		String accessKeyId = ConstantPropertiesUtil.ACCESS_KEY_ID;
		String accessKeySecret = ConstantPropertiesUtil.ACCESS_KEY_SECRET;
		String bucketName = ConstantPropertiesUtil.BUCKET_NAME;
		String fileHost = ConstantPropertiesUtil.FILE_HOST;
		String uploadUrl = null;
		// 创建OSSClient实例。
		OSS ossClient = new OSSClient(endPoint, accessKeyId, accessKeySecret);
		try {
			//判断是否存在该bucket
			if (!ossClient.doesBucketExist(bucketName)) {
				//创建一个bucket
				ossClient.createBucket(bucketName);
				//设置bucket为公有
				ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
			}
			//获取上传文件流
			InputStream inputStream = file.getInputStream();

			//构建日期路径
			String filePath = new DateTime().toString("yyyy/MM/dd");


			//获取文件源名称
			String original = file.getOriginalFilename();

			String fileName = UUID.randomUUID().toString();

			//获取文件类型
			String fileType = original.substring(original.indexOf('.'));

			String newName = fileName + fileType;

			String fileUrl = fileHost + "/" + filePath + "/" + newName;

			//文件上传到阿里云oss
			ossClient.putObject(bucketName, fileUrl, inputStream);

			// 关闭OSSClient。
			ossClient.shutdown();

			//获取url地址
			uploadUrl = "http://" + bucketName + "." + endPoint + "/" + fileUrl;

		} catch (IOException e) {
			e.printStackTrace();
		}

		return uploadUrl;
	}
}
